package com.smartwebx.fr.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.CharMatcher;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.smartwebx.fr.bean.ProcResultUtil;
import com.smartwebx.fr.bean.ShowDbFun;
import com.smartwebx.fr.bean.SimpleUseArgConf;
import com.smartwebx.fr.service.StoredProcService;
import com.smartwebx.fr.service.ckeditor.SmartTableService;
import com.smartwebx.fr.service.ckeditor.UseArgConfService;

@Controller
@RequestMapping("/table")
public class SmartTableController {

	@Autowired
	private StoredProcService storedProcService;

	@Autowired
	private UseArgConfService useArgConfService;
	@Autowired
	private SmartTableService tableService;

	@RequestMapping(value = "/getTable")
	@ResponseBody
	public Object getTable(Integer pageId, String code) throws Exception {
		List<SimpleUseArgConf> useArgConfs = useArgConfService.getSimpleUseArgConfs(pageId);
		List<List<String>> result = storedProcService.getResultBySqlAndPageParam(code, useArgConfs, "table", true);
		return result;
	}

	@RequestMapping(value = "/getProcTable")
	@ResponseBody
	public Object getProcTable(Integer pageId, Integer funId, String paramNameVal) throws Exception {
		Map<String, SimpleUseArgConf> paramNameUseArg = Maps.newHashMap();
		List<SimpleUseArgConf> useArgs = useArgConfService.getSimpleUseArgConfs(pageId);

		if (StringUtils.isNotBlank(paramNameVal)) {
			List<String> paramNameVals = Splitter.on(CharMatcher.anyOf("@|")).splitToList(paramNameVal);
			for (int i = 0; i < paramNameVals.size() / 2; i++) {
				String name = paramNameVals.get(i * 2);
				final String useParamId = paramNameVals.get(i * 2 + 1);

				SimpleUseArgConf useArg = Iterables.find(useArgs, new Predicate<SimpleUseArgConf>() {
					@Override
					public boolean apply(SimpleUseArgConf input) {
						return StringUtils.equals(input.getId(), useParamId);
					}
				}, null);
				if (useArg == null) {
					useArg = new SimpleUseArgConf();
					useArg.setName(name);
					useArg.setVal(useParamId);
				}
				paramNameUseArg.put(name, useArg);
			}
		}

		ProcResultUtil procUtl = tableService.getProTable(funId, paramNameUseArg);

		Map<String, Object> map = Maps.newHashMap();
		map.put("result", procUtl.getResult());
		map.put("columnInfos", procUtl.getColumnInfos());
		return map;
	}

	@RequestMapping(value = "/getDbProcs")
	@ResponseBody
	public Object getDbProcs(Integer returnTypeKey) throws Exception {
		Multimap<String, ShowDbFun> results = tableService.getDbProcs(returnTypeKey);

		return results.asMap();
	}
}